const express = require('express')
const history = require('connect-history-api-fallback')
const compression = require('compression')
const path = require('path')
const opn = require("opn")
const os = require('os')

// 监听的端口
const port = 8081

const app = express()

app.use(compression())

app.use(history({
  rewrites: [{
    from: /^\/api\/.*$/,
    to: function(context) {
      return context.parsedUrl.path;
    }
  }]
}))

app.use(express.static(path.join(__dirname, 'dist')))

app.listen(port, () => {

  let localhost = ""
  try {
    let network = os.networkInterfaces()
    localhost = network[Object.keys(network)[1]][1].address
  } catch (error) {
    localhost = "localhost"
  }
  const url = `http://${localhost}:${port}`

  console.log(`match listening port in ${port}, and url in ${url}`)

  opn(url)
})